package com.kaesun.ufit.hardware;

import java.io.File;
import java.io.FileOutputStream;

import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;

public class Buzzer {

	private static final String DEV_PATH = "/dev/buzzer";
	private static final int HALDER_BUZZER = 1;

	private static Handler mHanlder = new Handler() {
		public void handleMessage(Message msg) {
			if (HALDER_BUZZER == msg.what) {
				off();
			}
		}
	};

	public static void delay(int delay) {
		on();
		//mHanlder.sendEmptyMessageDelayed(HALDER_BUZZER, delay);
		mHanlder.sendMessageAtTime(mHanlder.obtainMessage(HALDER_BUZZER), (SystemClock.elapsedRealtime() + delay));
	}

	public static void on() {
		writeFile(DEV_PATH, "1");
	}

	public static void off() {
		writeFile(DEV_PATH, "0");
	}


	private static void writeFile(String devPath, String cmd) {
		File file = new File(devPath);
		try {
			new FileOutputStream(file).write(cmd.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
